package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.core.system.service.admin.IMovieService;
import com.hjx.movie.core.system.service.commons.file.FileService;
import com.hjx.movie.core.system.vo.FileVo;
import com.hjx.movie.persistence.system.model.IvyMovie;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;


/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/31 - 17:06
 */
@Slf4j
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Resource
    IMovieService movieService;

    @GetMapping("/findAll")
    public Result findAllMovie(){
        return Result.success(movieService.findAllMovie());
    }
    @GetMapping("/findMovieType")
    public Result findMovieType(){
        return Result.success(movieService.findAllMovieType());
    }
    @GetMapping("/findMovieArea")
    public Result findMovieArea(){
        return Result.success(movieService.findAllMovieArea());
    }

    @PostMapping("/add")
    public Result addMovie(@RequestBody IvyMovie movie){
        log.debug("新增电影信息入参为:{}",movie);
        if (movieService.addMovie(movie)){
            return Result.success("新增电影成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"新增电影失败");
    }
    @PostMapping("/update")
    public Result updateMovie(@RequestBody IvyMovie movie){
        log.debug("修改电影信息入参为:{}",movie);
        if (movieService.updateMovie(movie)){
            return Result.success("修改电影成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"修改电影失败");
    }

    @GetMapping("/delete")
    public Result updateMovie(@RequestParam("movieId")Long movieId){
        log.debug("删除电影入参为:{}",movieId);
        if (movieService.deleteMovie(movieId)){
            return Result.success("删除电影成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"删除电影失败");
    }

    @RequestMapping("uploadMovieRes")
    public Result fileUpload(@RequestHeader("movieId")Long movieId, MultipartFile file,@RequestHeader("type")String type) {
            log.debug("{}",movieId);
            String realPath = movieService.getRealPath(movieId,type);
            log.debug("realPath:{}",realPath);
            String name = FileService.fileUpload(file,realPath);
            String baseUrl = movieService.getMovieResHttpBaseUrl(type);
            String url = baseUrl+movieId+"/"+name;
            if (StringUtils.isNotBlank(type)&&type.equals("img")){
                movieService.updateMovie(IvyMovie.builder().movieId(movieId).imgPath(realPath).picture(url).build());
            }else if (StringUtils.isNotBlank(type)&&type.equals("video")){
                movieService.updateMovie(IvyMovie.builder().movieId(movieId).video(realPath).build());
            }
            FileVo fileVo = new FileVo();
            fileVo.setName(name);
            fileVo.setUrl(url);
            fileVo.setThumbUrl(url);
            return Result.success(fileVo);
    }
    // 查看
    @RequestMapping("movieResList")
    public Result movieResList(@RequestParam("movieId") Long movieId,@RequestParam("type")String type){
        String realPath = movieService.getRealPath(movieId,type);
        log.debug("realPath:{}",realPath);
        List<FileVo> moviePicFileList = movieService.getMovieResFileList(movieId,realPath,type);
        return Result.success(moviePicFileList);
    }

    @RequestMapping("deleteMovieRes")
    public Result deleteMovieRes(@RequestParam("movieId") Long movieId,@RequestParam("fileName")String fileName,@RequestParam("type")String type){
        String realPath = movieService.getRealPath(movieId,type);
        log.debug("realPath:{}",realPath);
        movieService.deleteMovieRes(fileName,realPath);
        return Result.success("删除剧照成功");
    }

    @GetMapping("queryMovieLike")
    public Result queryUserLike(@RequestParam(value = "movieLike") String str){
        if (StringUtils.isNotBlank(str)){
            return Result.success(movieService.queryMovieLike(str));
        }
        return Result.success(movieService.findAllMovie());
    }
}
